package com.llp.declaresystem.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.llp.declaresystem.domain.entity.*;
import com.llp.declaresystem.mapper.ExpertMapper;
import com.llp.declaresystem.mapper.UnitMapper;
import com.llp.declaresystem.mapper.UseradminMapper;
import com.llp.declaresystem.service.UserService;
import com.llp.declaresystem.mapper.UserMapper;
import com.llp.declaresystem.service.UsergroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author NASK
 * @description 针对表【user(统一登录用户信息表)】的数据库操作Service实现
 * @createDate 2022-11-23 16:22:57
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UnitMapper unitMapper;

    @Autowired
    private ExpertMapper expertMapper;

    @Autowired
    private UseradminMapper useradminMapper;

    @Autowired
    private UsergroupService usergroupService;

    @Override
    public Map login(User user, HttpSession session) {
        Map userInfo = userMapper.identityByUsername(user.getUsername());
        Integer uid = (Integer) userInfo.get("uid");
        Integer gid = (Integer) userInfo.get("gid");
        if (uid != -1) {
            //将该用户信息保存到session中
            Object obj =  saveUserInfo(session, uid,gid);
            userInfo.put("userInfo", obj);
            userInfo.remove("uid");
            return userInfo;
        }
        return null;
    }

    //返回对应admin的用户列表的list集合；
    @Override
    public List<AdminUserView> getAdminUserView() {
        List<AdminUserView> adminUserViews=new ArrayList<>();
        List<User> list = list();
        for(int i=0;i<list.size();++i)
        {
            User user = list.get(i);
            String gname  = usergroupService.getById(user.getGid()).getGname();
            if(user!=null&&gname!=null)
            {
                AdminUserView adminUserView=new AdminUserView(user.getUid(),user.getGid(),user.getUsername(),gname);
                adminUserViews.add(adminUserView);
            }
        }
        return adminUserViews;


    }

    //添加用户
    @Override
    public boolean addUser(User user) {
        //保存用户到user表
        boolean save = save(user);
        log.info("保存到用户表成功");
        Integer gid = user.getGid();
        Integer uid = user.getUid();
        if(gid==2)
        {
            Expert expert = new Expert();
            expert.setUid(uid);
            expert.setName("默认专家名");
            expertMapper.insert(expert);
            log.info("保存到专家表");

        }
        else if(gid==3)
        {
            Unit unit=new Unit();
            unit.setUid(uid);
            unit.setUnitname("默认单位名");
            unitMapper.insert(unit);
            log.info("保存到单位表");
        }
        return save;
    }

    public Object saveUserInfo(HttpSession session, Integer uid,Integer gid) {
        //管理员
        if (gid == 1) {
            Useradmin useradmin = useradminMapper.selectByUid(uid);
            session.setAttribute("userInfo", useradmin);
            return useradmin;
        } else if (gid == 2) {  //专家
            Expert expert = expertMapper.selectByUid(uid);
            session.setAttribute("userInfo", expert);
            return expert;
        } else {  //单位
            Unit unit = unitMapper.selectByUid(uid);
            session.setAttribute("userinfo", unit);
            return unit;
        }
    }
}




